package de.grosjohann.android.feuerwehr;

import java.io.Serializable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.domain.Fahrzeug;
import de.grosjohann.android.feuerwehr.location.ILocationResult;
import de.grosjohann.android.feuerwehr.location.MyLocation;
import de.grosjohann.android.feuerwehr.panel.Panel;
import de.grosjohann.android.feuerwehr.service.FMSAuthService;
import de.grosjohann.android.feuerwehr.service.FMSUpdateService;
import de.grosjohann.android.feuerwehr.util.LocationUtil;

public class StatusActivity
  extends Activity
  implements View.OnClickListener, IUpdateUIListener {

  public static final String FMS_OBJECT = "feuerwehr.FMS_OBJECT";

  private Fahrzeug fahrzeug;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.status);
    setTitle(R.string.lblStatusChange);

    Button btnStatus1 = (Button) findViewById(R.id.btnStatus1);
    btnStatus1.setOnClickListener(this);

    Button btnStatus2 = (Button) findViewById(R.id.btnStatus2);
    btnStatus2.setOnClickListener(this);

    Button btnStatus3 = (Button) findViewById(R.id.btnStatus3);
    btnStatus3.setOnClickListener(this);

    Button btnStatus4 = (Button) findViewById(R.id.btnStatus4);
    btnStatus4.setOnClickListener(this);

    Button btnStatus5 = (Button) findViewById(R.id.btnStatus5);
    btnStatus5.setOnClickListener(this);

    Button btnStatus6 = (Button) findViewById(R.id.btnStatus6);
    btnStatus6.setOnClickListener(this);

    Button btnStatus0 = (Button) findViewById(R.id.btnStatus0);
    btnStatus0.setOnClickListener(this);

    Button button = (Button) findViewById(R.id.btnStatusCancel);
    button.setOnClickListener(this);

    FMSUpdateService.updateUIListener = this;
  }

  @Override
  public void onResume() {
    super.onResume();

    Bundle b = getIntent().getExtras();
    if (b != null) {
      Serializable serObj = b.getSerializable(FMS_OBJECT);
      if (serObj instanceof Fahrzeug) {
        fahrzeug = (Fahrzeug) serObj;

        TextView tvDescription = (TextView) findViewById(R.id.tvStatusDescription);
        tvDescription.setText(fahrzeug.getDescription());
        TextView tvKennung = (TextView) findViewById(R.id.tvStatusKennung);
        tvKennung.setText(fahrzeug.getIdentifier());
      }
    }
  }

  @Override
  protected void onStop() {
    super.onStop();
    fahrzeug = null;
  }

  @Override
  public void onClick(View view) {

    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    Intent intent = new Intent(this, FMSUpdateService.class);
    intent.putExtra(FMSAuthService.FMS_AUTHORIZATION, tm.getDeviceId());
    intent.putExtra(FMSUpdateService.FMS_STATUS_KENNUNG, fahrzeug.getIdentifier());

    switch (view.getId()) {
      case R.id.btnStatus2:
        intent.putExtra(FMSUpdateService.FMS_LOC_DATA, LocationUtil.LOC_HOME);

      case R.id.btnStatus6:
        if (!intent.hasExtra(FMSUpdateService.FMS_LOC_DATA)) {
          intent.putExtra(FMSUpdateService.FMS_LOC_DATA, "");
        }

      case R.id.btnStatus0:
      case R.id.btnStatus1:
      case R.id.btnStatus3:
      case R.id.btnStatus4:
      case R.id.btnStatus5:

        Button btnStatus = (Button) view;
        String status = (String) btnStatus.getText();
        intent.putExtra(FMSUpdateService.FMS_STATUS_DATA, status);
        startService(intent);
        break;

      case R.id.btnStatusCancel:
      default:
        finish();
        break;

    }
  }

  @Override
  public void updateUI(String data) {
    // nothing to do      
  }

  @Override
  public void showInfo(String info) {
    finish();
  }

  @Override
  public void showWarning(String warning) {
    // nothing to do        
  }

  @Override
  public void showError(final String error) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Panel panel = new Panel(getApplicationContext(), StatusActivity.this);
        panel.showError(R.string.errSendFMSData);
      }
    });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.location, menu);
    return true;
  }

  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    if (fahrzeug.getLocation() == null || fahrzeug.getLocation().isEmpty()) {
      menu.findItem(R.id.miLocSearch).setEnabled(false);
    }
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    Context ctx = getApplicationContext();
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    final Intent intent = new Intent(this, FMSUpdateService.class);
    intent.putExtra(FMSAuthService.FMS_AUTHORIZATION, tm.getDeviceId());
    intent.putExtra(FMSUpdateService.FMS_STATUS_KENNUNG, fahrzeug.getIdentifier());

    switch (item.getItemId()) {
      case R.id.miLocCurrent:
        MyLocation myLocation = new MyLocation(ctx);

        myLocation.getLocation(new ILocationResult() {

          @Override
          public void gotLocation(Location location) {
            if (location != null) {
              intent.putExtra(FMSUpdateService.FMS_LOC_DATA, LocationUtil.toString(location));
              startService(intent);
            }
          }
        });

        return true;

      case R.id.miLocHome:
        intent.putExtra(FMSUpdateService.FMS_LOC_DATA, LocationUtil.LOC_HOME);
        startService(intent);
        return true;

      case R.id.miLocSearch:
        Intent mapsIntent = new Intent(this, MapsActivity.class);
        mapsIntent.putExtra(MapsActivity.OBJECT, fahrzeug);
        mapsIntent.putExtra(MapsActivity.LOCATION, fahrzeug.getLocation());
        startActivity(mapsIntent);

        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }
}